﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Portfolio.IViews.Admin;
using Portfolio.Presenters.Admin;
using Portfolio.Entities;
using CKEditor.NET;

namespace Portfolio.UI.Admin.Controls
{
    public partial class AdminEditArticleDetailsViewControl : System.Web.UI.UserControl, IAdminEditArticleView
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (LoadArticle != null)
                {
                    if (IdArticle == 0)
                    {
                        dvEditArticle.ChangeMode(DetailsViewMode.Insert);
                    }
                    else
                    {
                        dvEditArticle.ChangeMode(DetailsViewMode.Edit);
                    }
                    LoadArticle(this, EventArgs.Empty);
                }
                if (LoadCategories != null)
                {
                    LoadCategories(this, EventArgs.Empty);
                }

                if (LoadStatus != null)
                {
                    LoadStatus(this, EventArgs.Empty);
                }

                IsArticleInsert = false;
                IsArticleUpdated = false;
            }

            if (Session["message_insert"] != null && (Int32)Session["message_insert"] == 1)
            {
                IsArticleInsert = true;
                Session["message_insert"] = null;                
            }

            if (Session["message_error"] != null)
            {
                PublishException = Session["message_error"].ToString();
                Session["message_error"] = null;
            }

            
           
        }

        protected void imgbtnSaveExit_Click(object sender, EventArgs e)
        {
            switch (IdArticle)
            {
                case 0:
                    if (InsertExitArticle != null)
                    {
                        InsertExitArticle(this, EventArgs.Empty);                        
                    }
                break;

                default:
                    if (UpdateArticle != null)
                    {
                        UpdateArticle(this, EventArgs.Empty);
                    }
                break;
            }

            if ((IsArticleInsert == true) || (IsArticleUpdated == true))
            {
                this.divError.Visible = false;
            }

            if (!this.divError.Visible)
            {
                Session["message_error"] = 1;
                Response.Redirect("~/Admin/AdminArticles.aspx");
            }
        }

        protected void imgbtnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/AdminArticles.aspx");
        }

        protected void imgbtnSave_Click(object sender, ImageClickEventArgs e)
        {
            IsArticleInsert = false;
            IsArticleUpdated = false;
            switch (IdArticle)
            {
                case 0:
                    if (InsertArticle != null)
                    {
                        InsertArticle(this, EventArgs.Empty);
                    }
                    break;

                default:
                    if (UpdateArticle != null)
                    {
                        UpdateArticle(this, EventArgs.Empty);
                    }
                    break;
            }
            if (IsArticleInsert == true)
            {
                Session["message_insert"] = 1;
            }
            else
            if (IsArticleInsert == false)
            {
                Session["message_insert"] = 0;
            }            
        }

        #region IAdminEditArticleDetailsView members

        public event EventHandler<EventArgs> LoadArticle;
        public event EventHandler<EventArgs> LoadCategories;
        public event EventHandler<EventArgs> LoadStatus;
        public event EventHandler<EventArgs> UpdateArticle;
        public event EventHandler<EventArgs> InsertArticle;
        public event EventHandler<EventArgs> InsertExitArticle;

        public IList<ArtilcesGridV> ArticleDataBind
        {
            set 
            {
                dvEditArticle.DataSource = value;
                dvEditArticle.DataBind();
            }
        }

        public ArticleT AtriclesData
        {
            get
            {                
                ArticleT _articleT = new ArticleT();
                _articleT.ArticleID = IdArticle;
                DropDownList _ddlCategories = (DropDownList)dvEditArticle.FindControl("ddlCategories");
                _articleT.ArticleCategoryID = Int32.Parse(_ddlCategories.SelectedValue);
                _ddlCategories = (DropDownList)dvEditArticle.FindControl("ddlStatus");
                _articleT.Status = Int16.Parse(_ddlCategories.SelectedValue);
                CheckBox _cbActiveFlag = (CheckBox)dvEditArticle.FindControl("cbActiveFlag");
                _articleT.ActiveFlag = _cbActiveFlag.Checked;
                _articleT.ModifiedDate = DateTime.Now;
                TextBox _txtTemp = (TextBox)dvEditArticle.FindControl("txtTitle");
                _articleT.Title = _txtTemp.Text;
                _txtTemp = (TextBox)dvEditArticle.FindControl("txtSubTitle");
                _articleT.SubTitle = _txtTemp.Text;
                
                _txtTemp = (TextBox)dvEditArticle.FindControl("txtRegisterDate");
                //_articleT.RegisterDate = DateTime.ParseExact(_txtTemp.Text, "yyyy-MM-dd HH:mm:tt", null);
                _articleT.RegisterDate = DateTime.Parse(_txtTemp.Text);                             

                CKEditorControl _ckeTemp = (CKEditorControl)dvEditArticle.FindControl("ckeText");
                _articleT.Text = _ckeTemp.Text;                

                return _articleT;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int IdArticle
        {
            get
            {
                if (Request.QueryString["articleId"] != null)
                {
                    return Int32.Parse(Request.QueryString["articleId"]);
                }
                return 0;                
            }
            set
            {
                Response.Redirect("~/Admin/AdminEditArticle.aspx?articleId=" + value, false);
            }
        }

        public bool IsArticleUpdated
        {
            set
            {
                //if (value)
                //{
                //    divSuccess.Visible = value;
                //}
                divSuccess.Visible = value;
            }

            get
            {
                return divSuccess.Visible; 
            }
        }

        public bool IsArticleInsert
        {
            set
            {                
                divSuccessIns.Visible = value;
            }
            get
            {
                return divSuccessIns.Visible; 
            }
        }

        public string PublishException
        {
            set
            {
                this.divError.Visible = true;
                this.valCustom.ErrorMessage = value;
                this.valCustom.IsValid = false;
                this.IsArticleUpdated = false;
                this.IsArticleInsert = false;
                Session["message_error"] = value;
            }
        }

        public IViews.Common.DataSourceContainer CategoriesDataSource
        {
            set
            {                
                DropDownList ddlCategories = (DropDownList)dvEditArticle.FindControl("ddlCategories");
                ddlCategories.DataSource = value.DataSource;
                ddlCategories.DataTextField = value.DataTextFieldName;
                ddlCategories.DataValueField = value.DataValueFieldName;                
                ddlCategories.DataBind();
                if (value.DataSelectedValue != 0)
                {
                    ddlCategories.Items.FindByValue(value.DataSelectedValue.ToString()).Selected = true;
                }
            }
        }

        public IViews.Common.DataSourceContainer StatusDataSource
        {
            set
            {
                DropDownList ddlStatus = (DropDownList)dvEditArticle.FindControl("ddlStatus");
                ddlStatus.DataSource = value.DataSource;
                ddlStatus.DataTextField = value.DataTextFieldName;
                ddlStatus.DataValueField = value.DataValueFieldName;
                ddlStatus.DataBind();
                if (value.DataSelectedValue != null)
                {
                    ddlStatus.Items.FindByValue(value.DataSelectedValue.ToString()).Selected = true;
                }
            }
        }

        #endregion      

        
    }
}